package com.lwj.liwanjia.view.activity.search;


import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.utils.RxBus;
import com.lwj.liwanjia.view.fragment.search.SearchHistoryFragment;
import com.lwj.liwanjia.view.fragment.search.SearchListFragment;
import com.lwj.liwanjia.view.fragment.search.SearchListFragment1;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.search)
    EditText search;
    @BindView(R.id.search_type)
    TextView searchType;

    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Fragment mFragment;

    @Override
    protected void initView() {
        getFragments();
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    if (searchType.getText().toString().equals("宝贝")) {
                        switchFragment(fragmentList.get(1));
                    } else {
                        switchFragment(fragmentList.get(2));
                    }
                } else {
                    switchFragment(fragmentList.get(0));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    public String getType() {
        return searchType.getText().toString().trim();
    }

    public EditText getSearch() {
        return search;
    }

    private void getFragments() {
        fragmentList.add(new SearchHistoryFragment());
        fragmentList.add(new SearchListFragment());
        fragmentList.add(new SearchListFragment1());
        mFragment = fragmentList.get(0);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_search, mFragment).commit();
    }

    /**
     * 搜索界面切换
     *
     * @param fragment
     */
    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是否就是切换的Fragment
        if (mFragment != fragment) {
            //判断切换的fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，先把当前的fragment隐藏，再添加新的fragment
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.fragment_search, fragment).commit();
            } else {
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @OnClick({R.id.clear_image, R.id.tv_cancel, R.id.search_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear_image://清空搜索内容
                String s = search.getText().toString();
                if (!TextUtils.isEmpty(s)) {
                    search.setText("");
                }
                break;
            case R.id.tv_cancel://取消
                finish();
                break;
            case R.id.search_type://选择搜索的类型
                View selectView = LayoutInflater.from(this).inflate(R.layout.select_pop, null);
                final TextView tvBaobei = (TextView) selectView.findViewById(R.id.tv_baobei);
                TextView tvDianpu = (TextView) selectView.findViewById(R.id.tv_dianpu);
                final PopupWindow popupWindow = new PopupWindow(selectView, LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
                //实例化一个ColorDrawable,点击窗体外部区域时消失
                ColorDrawable cd = new ColorDrawable();
                popupWindow.setBackgroundDrawable(cd);
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(searchType);
                tvBaobei.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchType.setText("宝贝");
                        search.setText("");
                        RxBus.getInstance().post("宝贝");
                        popupWindow.dismiss();
                    }
                });
                tvDianpu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchType.setText("店铺");
                        search.setText("");
                        RxBus.getInstance().post("店铺");
                        popupWindow.dismiss();
                    }
                });
                break;
        }
    }
}
